package com.dhcc.sds.base.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.sys.dao.CdcProductMapper;
import com.dhcc.cdc.sys.dao.DictionaryMapper;
import com.dhcc.cdc.sys.dao.DictypeMapper;
import com.dhcc.cdc.sys.entity.CdcProductEntity;
import com.dhcc.cdc.sys.entity.DictionaryEntity;
import com.dhcc.cdc.sys.entity.DictypeEntity;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.sds.base.dao.SdsExpressMapper;
import com.dhcc.sds.base.dao.SdsQcentityMapper;
import com.dhcc.sds.base.dao.SdsQcentityRuleMapper;
import com.dhcc.sds.base.entity.SdsExpressEntity;
import com.dhcc.sds.base.entity.SdsQcentityEntity;
import com.dhcc.sds.base.entity.SdsQcentityRuleEntity;
import com.dhcc.sds.base.service.ISdsQcentityRuleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 病种筛查配置表数据服务层接口
 * 
 * @ClassName: SdsQcentityRuleServiceImpl
 * @Description: TODO
 * @author sunyingjie
 * @date 2021-03-22 10:10:49
 */
@Service("sdsQcentityRuleService")
public class SdsQcentityRuleServiceImpl extends BaseServiceImpl<SdsQcentityRuleMapper, SdsQcentityRuleEntity> implements ISdsQcentityRuleService {

	@Autowired
	private SdsQcentityMapper sdsQcentityMapper;
	@Autowired
	private SdsExpressMapper sdsExpressMapper;
	@Autowired
	private CdcProductMapper productMapper;
	@Autowired
	private DictypeMapper dictypeMapper;
	@Autowired
	private DictionaryMapper dictionaryMapper;

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public List<SdsQcentityRuleEntity> page(Page<SdsQcentityRuleEntity> page, Map<String, Object> map) {
		if ("0".equals(map.get("entityId"))) {
			return new ArrayList<>();
		}
		return this.baseMapper.page(page, map);
	}
	@Override
	public List<SdsQcentityRuleEntity> list(Map<String, Object> map) {
		return this.baseMapper.list(map);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void importRule(List<List<Object>> all) {
		Map<String, Long> qcEntiytMap = new HashMap<>();
		for (int i = 1; i < all.size(); i++) {
			List<Object> ls = all.get(i);
			String qcAbbrev = ls.get(0).toString(); // 排序
			String ruleType = CommonUtil.equals(ls.get(1),"准入")?"1":"2"; // 1：准入 2：排除
			String ruleDesc = ls.get(2).toString(); // 规则描述
			String ruleMethodDesc = "";  // 函数描述
			String param1 = ""; // 参数
			String param2 = ""; // 参数2
			try {
				ruleMethodDesc = ls.get(3).toString();
			} catch (Exception e){
				logger.error("导入病种规则出错：第"+i+"行，描述字段" + e);
			}
			try {
				param1 = ls.get(4).toString();
			} catch (Exception e){
				logger.error("导入病种规则出错：第"+i+"行，参数字段" + e);
			}
			try {
				param2 = ls.get(6).toString().trim();
			} catch (Exception e){
				logger.error("导入病种规则出错：第"+i+"行，参数2字段" + e);
			}
			Integer indNo = Integer.valueOf(ls.get(5).toString()); // 排序

			// 如果当前病种没有批量置为无效
			if (qcEntiytMap.get(qcAbbrev)==null) {
				// 获取病种信息
				SdsQcentityEntity qcEntity = new SdsQcentityEntity();
				qcEntity.setAbbrev(qcAbbrev);
				qcEntity = this.sdsQcentityMapper.selectOne(qcEntity);
				if (qcEntity==null) {
					continue;
				}
				Wrapper<SdsQcentityRuleEntity> wpr = new EntityWrapper<>();
				wpr.eq("entity_id", qcEntity.getId()).eq("is_only", 0);
				this.baseMapper.updateForSet("is_active=0", wpr);

				qcEntiytMap.put(qcAbbrev, qcEntity.getId());
			}

			Long entityId = qcEntiytMap.get(qcAbbrev); // 病种Id

			CdcProductEntity productEntity = new CdcProductEntity();
			productEntity.setCode("SDS");
			productEntity = productMapper.selectOne(productEntity);
			// 通过函数描述获取函数id
			Long methodId = null;
			if (CommonUtil.isNotEmpty(ruleMethodDesc)) {
				if (ruleMethodDesc.contains("手术")) {
					if (CommonUtil.equals(ruleMethodDesc, "手术")) {
						ruleMethodDesc = "手术";
					} else {
						ruleMethodDesc = "主要手术";
					}
				} else if (ruleMethodDesc.contains("操作")) {
					ruleMethodDesc = "操作";
				}
				DictypeEntity dicType = new DictypeEntity();
				dicType.setProductId(productEntity.getId());
				dicType.setCode("SDExpType");
				dicType = dictypeMapper.selectOne(dicType);
				DictionaryEntity dictionary = new DictionaryEntity();
				dictionary.setCode("QCRule");
				dictionary.setDictypeId(dicType.getId());
				dictionary =  dictionaryMapper.selectOne(dictionary);
				Map<String,Object> map = new HashMap<>();
				map.put("expType", dictionary.getId());
				map.put("expDesc", ruleMethodDesc);
				List<SdsExpressEntity> explist = sdsExpressMapper.list(map);
				if (explist.size()!=0) {
					methodId = explist.get(0).getId();
				}
			}

			// 判断规则是否重复
			SdsQcentityRuleEntity rule = new SdsQcentityRuleEntity();
			rule.setEntityId(entityId);
			rule.setRuleDesc(ruleDesc);
			rule.setIsOnly(0);
			rule = this.baseMapper.selectOne(rule);
			if (rule == null) {
				rule = new SdsQcentityRuleEntity();
				rule.setEntityId(entityId);
				rule.setRuleType(ruleType);
				rule.setRuleDesc(ruleDesc);
				rule.setExpressId(methodId);
				rule.setParam(param1);
				rule.setParam2(param2);
				rule.setIndNo(indNo);
				rule.setIsActive(1);

				rule.insert();
			} else {
				rule.setRuleType(ruleType);
				rule.setExpressId(methodId);
				rule.setParam(param1);
				rule.setParam2(param2);
				rule.setIndNo(indNo);
				rule.setIsActive(1);

				rule.updateAllColumnById();
			}
		}
	}
}